package org.bigdata.framework.core.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bigdata.framework.core.utils.DateUtils;
import org.bigdata.framework.core.utils.Pinyin4jUtils;
import org.json.JSONArray;
import org.bigdata.framework.admin.web.ueditor.ActionEnter;
import org.bigdata.framework.core.model.AnswerVO;
import org.bigdata.framework.core.model.Menu;
import org.bigdata.framework.core.model.Permission;
import org.bigdata.framework.core.model.Role;
import org.bigdata.framework.core.model.User;
import org.bigdata.framework.core.model.UserRole;
import org.bigdata.framework.core.protocol.IBaseService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户 管理 
 */
@ApiIgnore
@Controller
public class SystemController extends BaseController {

	@Autowired
	private IPublicService publicService;
	@Autowired
	private IBaseService baseService;
	
	private static String FILESERVER = "";
	private static String FILEPATH = "";

	static {
		Properties properties_config = new Properties();
		InputStream in_config = SystemController.class.getClassLoader().getResourceAsStream("config.properties");
		try {
			properties_config.load(in_config);

			FILESERVER = properties_config.getProperty("file.server");
			FILEPATH = properties_config.getProperty("file.path");
		} catch (IOException e) {
		}
	}
	

	//菜单管理
	@RequestMapping("user-menu-manage.html")
	public String toKnowledgeEditDataUser() {
		return "/modules/repository/knowledge_edit_data_menu";
	}
	//菜单管理树
	@RequestMapping("knowledge-edit-data-menu-zTree.html")
	public String toKnowledgeEditDataMenuzTree() {
		return "/modules/repository/knowledge_edit_data_menu_zTree";
	}
	//添加菜单
	@RequestMapping("knowledge-edit-data-addmenu.html")
	public String toKnowledgeEditDataAddMenu() {
		return "/modules/repository/knowledge_edit_data_addmenu";
	}
	//修改菜单
	@RequestMapping("knowledge-edit-data-uploadmenu.html")
	public String toKnowledgeEditDataUploadMenu() {
		return "/modules/repository/knowledge_edit_data_uploadmenu";
	}
	//添加菜单按钮
	@RequestMapping("knowledge-edit-data-addmenubutton.html")
	public String toKnowledgeEditDataAddMenuButton() {
		return "/modules/repository/knowledge_edit_data_addmenubutton";
	}
	//详情菜单按钮
	@RequestMapping("knowledge-edit-data-querymenubutton.html")
	public String toKnowledgeEditDataQueryButton() {
		return "/modules/repository/knowledge_edit_data_querymenubutton";
	}
	
	//权限管理
	@RequestMapping("user-menu-jurisdiction.html")
	public String toKnowledgeEditDataJurisdiction() {
		return "/modules/repository/knowledge_edit_data_jurisdiction";
	}
	//角色管理
	@RequestMapping("role.html")
	public String toKnowledgeEditDataRole() {
		return "/modules/repository/knowledge_edit_data_role";
	}
	//查询用户
	@RequestMapping("knowledge-edit-data-queryuser.html")
	public String toKnowledgeEditDataQueryUser() {
		return "/modules/repository/knowledge_edit_data_queryuser";
	}
	//查询用户详情
	@RequestMapping("knowledge-edit-data-queryuserdetails.html")
	public String toKnowledgeEditDataQueryUserDetails() {
		return "/modules/repository/knowledge_edit_data_userdetails";
	}
	
	//增加用户组
	@RequestMapping("knowledge-edit-data-adduser.html")
	public String toKnowledgeEditDataaddUser() {
		return "/modules/repository/knowledge_edit_data_adduser";
	}
	
	//增加用户组树状
	@RequestMapping("knowledge-edit-data-usergroup-zTree.html")
	public String toKnowledgeEditDataUserGroupzTree() {
		return "/modules/repository/knowledge_edit_data_usergroup_zTree";
	}
	//用户组管理
	@RequestMapping("user-menu-rolegroup.html")
	public String toKnowledgeEditDataRoleGroup() {
		return "/modules/repository/knowledge_edit_data_rolegroup";
	}
	//修改用户组页面
	@RequestMapping("knowledge-edit-data-uploadroleGroup.html")
	public String toKnowledgeEditDataUploadRole() {
		return "/modules/repository/knowledge_edit_data_uploadrole";
	}
	//添加用户
	@RequestMapping("knowledge-edit-data-addrole.html")
	public String toKnowledgeEditDataAddRole() {
		return "/modules/repository/knowledge_edit_data_addrole";
	}
	//添加角色
	@RequestMapping("knowledge-edit-data-saverole.html")
	public String toKnowledgeEditDataSaveRole() {
		return "/modules/repository/knowledge_edit_data_saverole";
	}
	//部門管理
	@RequestMapping("user-menu-section.html")
	public String toKnowledgeEditDataSection() {
		return "/modules/repository/knowledge_edit_data_section";
	}
	//添加部門
	@RequestMapping("knowledge-edit-data-adddepartment.html")
	public String toKnowledgeEditDataAdddepartment() {
		return "/modules/repository/knowledge_edit_data_adddepartment";
	}
	//部門树
	@RequestMapping("knowledge-edit-data-department-zTree.html")
	public String toKnowledgeEditDataDepartmentzTree() {
		return "/modules/repository/knowledge_edit_data_department_zTree";
	}

	
	
	

	/**
	 * 
	 * 角色
	 */
	
		//查询角色
		@RequestMapping("knowledgeEditDataRoleQuery")
		public void toKnowledgeEditDataRoleQuery(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String name = request.getParameter("name");
			String description = request.getParameter("description");
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			if(name !=null && !"".equals(name)){
				queryParams.put("=,name", name);
			}
			if(description !=null && !"".equals(description)){
				queryParams.put("=,description", description);
			}
			List<Role> role=publicService.getRole(queryParams);
			for(int i=0; i<role.size();i++){
				if(role.get(i).getId() == 1){
					role.remove(i);
				}
			}
			String resultString = formatToJson(role);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//修改查询角色
		@RequestMapping("knowledgeEditDataRoleEditQuery")
		public void toKnowledgeEditDataRoleEditQuery(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			Role role = publicService.findRole(id);
			LinkedHashMap<String, Object> roleList = new LinkedHashMap<String, Object>();
			roleList.put("id", role.getId());
			roleList.put("name", role.getName());
			roleList.put("description", role.getDescription());
			String resultString = formatToJson(roleList);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//添加角色
		@RequestMapping("knowledgeEditDataRoleAdd")
		public void toKnowledgeEditDataRoleAdd(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String name =request.getParameter("name");
			String description =request.getParameter("description");
			HashMap<String, String>r1=new HashMap<String, String>();
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,name", name);
			List<Role> role2 = publicService.getRole(queryParams );
			if(role2!=null&&role2.size()>0){
				r1.put("result", "fail");
				r1.put("msg", "角色名称已存在");
			}else{
				Role role = new Role();
				role.setName(name);
				role.setDescription(description);
				try {
					baseService.saveRole(role);
					r1.put("result", "success");
					r1.put("msg", "保存成功");
				} catch (Exception e) {
					r1.put("result", "fail");
					r1.put("msg", "保存失败");
					e.printStackTrace();
				}
			}
			
			
			
			String resultString = formatToJson(r1);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//删除角色
		@RequestMapping("knowledgeEditDataRoleDetele")
		public void toKnowledgeEditDataRoleDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			
			HashMap<String, String>r=new HashMap<String, String>();
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			try {
				
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,roleId", id);
				List<UserRole> userRoles = publicService.getUserRole(queryParams );
				if(userRoles!=null&&userRoles.size()>0){
					r.put("result", "fail");
					r.put("msg", "有关联用户，不能删除");
				}else{
					Role role = publicService.findRole(id);
					baseService.deleteRole(role);
					r.put("result", "success");
					r.put("msg", "操作成功");
				}
			} catch (Exception e) {
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】角色【"+role.getName()+"】失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
				r.put("result", "fail");
				r.put("msg", "操作失败");
			}
			if(logTime==0){
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】角色【"+role.getName()+"】成功！", 0);
			}
			
			String resultString = formatToJson(r);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//修改角色
		@RequestMapping("knowledgeEditDataRoleEdit")
		public void toKnowledgeEditDataRoleEdit(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			String name =request.getParameter("name");
			String description =request.getParameter("description");
			
			HashMap<String, String>r1=new HashMap<String, String>();
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,name", name);
			List<Role> role2 = publicService.getRole(queryParams );
			if(role2!=null&&role2.size()>1){
				r1.put("result", "fail");
				r1.put("msg", "角色名称已存在");
			}else{
				Role role = publicService.findRole(id);
				if(role!=null){
					role.setName(name);
					role.setDescription(description);
					try {
						baseService.editRole(role);
						r1.put("result", "success");
						r1.put("msg", "保存成功");
					} catch (Exception e) {
						r1.put("result", "fail");
						r1.put("msg", "保存失败");
						e.printStackTrace();
					}
				}else{
					r1.put("result", "fail");
					r1.put("msg", "不能进行编辑角色不存在");
				}
				
			}
			
			String resultString = formatToJson(r1);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//查询用户
		@RequestMapping("knowledgeEditDataRoleQueryUser")
		public void toKnowledgeEditDataRoleQueryUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String StringId =request.getParameter("id");
			Integer pageSize = Integer.parseInt(request.getParameter("page"));
			String username = request.getParameter("username");
			String telephone = request.getParameter("telephone");
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			if(username !=null && !"".equals(username)){
				queryParams.put("=,username", username);
			}
			if(telephone !=null && !"".equals(telephone)){
				queryParams.put("=,telephone", telephone);
			}
			String uid = request.getSession().getAttribute("userid")+"";
			if(!"1".equals(uid)){
				queryParams.put("!,username", "root");
			}
			Integer count = publicService.getUserCount(queryParams);
			int ye = (int) Math.ceil(Double.valueOf(count) / 5);
			
			List<Integer> userList = new ArrayList<Integer>();
			List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
			LinkedHashMap<String, Object> mp = null;
			List<UserRole> userRole= publicService.getUserRole();
				for(UserRole ur : userRole){
					if(StringId != null &&ur.getRoleId() == Integer.parseInt(StringId)){
						userList.add(ur.getUserId());
					}
				}
			List<User> user =publicService.getUser(queryParams, pageSize,5);
			
			for(User u :user){
				mp = new LinkedHashMap<String, Object>();
				for(int ul :userList){
					if(u.getId() == ul){
						mp.put("page", 1);
					}
				}
				mp.put("id", u.getId());
				mp.put("name", u.getUsername());
				mp.put("nickname", u.getNickName());
				mp.put("telephone", u.getTelephone());
				mp.put("email", u.getEmail());
				mp.put("pagination", ye);
				r.add(mp);
				}
			String resultString = formatToJson(r);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		
		//删除用户
		@RequestMapping("knowledgeEditDataDeleteUser")
		public void toKnowledgeEditDataDeleteUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			UserRole userRole=publicService.findUserRole(id);
			Role role= publicService.findRole(userRole.getRoleId());
			User u=publicService.findUser(userRole.getUserId());
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			try {
				baseService.deleteUserRole(userRole);
			} catch (Exception e) {
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】成功！", 0);
			}
			
			String resultString = formatToJson(1);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//删除全部用户
		@RequestMapping("knowledgeEditDataDeleteAllUser")
		public void toKnowledgeEditDataDeleteAllUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			List<UserRole> userRole= publicService.getUserRole();
			for(UserRole ur : userRole){
				if(ur.getRoleId() == id){
					int logTime=0;
					User u=publicService.findUser(ur.getUserId());
					Role role= publicService.findRole(ur.getRoleId());
					User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
					try {
						baseService.deleteUserRole(publicService.findUserRole(ur.getId()));
					} catch (Exception e) {
//						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
//						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】成功！", 0);
					}
				}
			}
			String resultString = formatToJson(1);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		//修改用户
		@RequestMapping("knowledgeEditDataAddUser")
		public void toKnowledgeEditDataAddUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			String[] button =request.getParameterValues("roleId");
			List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
			LinkedHashMap<String, Object> mp = null;
			
			if(button != null){
				for(int i=0; i<button.length;i++){
					List<UserRole> userRole= publicService.getUserRole();
						int suerroleid =00;
						for(UserRole ur : userRole){
							if(ur.getRoleId() == id && ur.getUserId() == Integer.parseInt(button[i])){
								suerroleid=ur.getId();
							}
						}
						if(suerroleid == 00){
							int logTime=0;
							User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
							UserRole userrole = new UserRole();
							userrole.setUserId(Integer.parseInt(button[i]));
							userrole.setRoleId(id);
							User u=publicService.findUser(Integer.parseInt(button[i]));
							Role role= publicService.findRole(id);
							try {
								baseService.saveUserRole(userrole);
							} catch (Exception e) {
//								this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】失败！请检查数据是否正确！", 1);
								logTime=1;
								e.printStackTrace();
							}
							if(logTime==0){
//								this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【"+role.getName()+"】角色下用户【"+u.getUsername()+"】成功！", 0);
							}
						}
				}
			}
			List<UserRole> userRole= publicService.getUserRole();
 			List<Integer> userList = new ArrayList<Integer>();
			for(UserRole ur : userRole){
				if(ur.getRoleId() == id){
					userList.add(ur.getUserId());
				}
			}
			for(int usid: userList){
				User user=publicService.findUser(usid);
				mp = new LinkedHashMap<String, Object>();
				for(UserRole ur : userRole){
					if(ur.getUserId() == usid ){
						mp.put("userRoleId", ur.getId());
					}
				}
				mp.put("userId", user.getId());
				mp.put("name", user.getUsername());
				r.add(mp);
			}	
			

			String result = formatToJson(r);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		
		//用户组查询用户
		@RequestMapping("knowledgeEditDataQueryUser")
		public void toKnowledgeEditDataQueryUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			
			int id =Integer.parseInt(request.getParameter("id"));
			List<Integer> userList = new ArrayList<Integer>();
			List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
			LinkedHashMap<String, Object> mp = null;
			List<UserRole> userRole= publicService.getUserRole();
				for(UserRole ur : userRole){
					if(ur.getRoleId() == id){
						userList.add(ur.getUserId());
					}
				}
				for(int ul :userList){
					mp = new LinkedHashMap<String, Object>();
					User ur =publicService.findUser(ul);
					mp.put("id", ur.getId());
					mp.put("name", ur.getUsername());
					r.add(mp);
				}
			String resultString = formatToJson(r);
			super.pringWriterToPage(resultString, "application/json", response);
		}
		
		
		
		
		

		
		
		/**
		 * 菜单
		 * 
		 */
		
		//查询菜单
		@RequestMapping("knowledgeEditDataQueryMent")
		public void toKnowledgeEditDataQueryMent(HttpServletResponse response) throws Exception {
			
			List<Menu> menu = publicService.getMenu();
			List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
			LinkedHashMap<String, Object> mp = null;
			for (int i = 0; i < menu.size(); i++) {
				Menu m = menu.get(i);
					mp = new LinkedHashMap<String, Object>();
					mp.put("id", m.getId());
					mp.put("name", m.getName());
					mp.put("pId", m.getParentId());
					r.add(mp);
			}
			String result = formatToJson(r);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		//添加菜单
		@RequestMapping("knowledgeEditDataAddMent")
		public void toKnowledgeEditDataAddMent(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String name =request.getParameter("name");
			String description =request.getParameter("description");
			String url =request.getParameter("url");
			String headline =request.getParameter("headline");
			int sort =Integer.parseInt(request.getParameter("sort"));
			int status =Integer.parseInt(request.getParameter("stauts"));
			int parentId =Integer.parseInt(request.getParameter("parentid"));
			Menu menu = new Menu();
			menu.setName(name);
			menu.setDescription(description);
			menu.setUrl(url);
			menu.setLevel(1);
			menu.setSort(sort);
			menu.setIcon(headline);
			menu.setStatus(status);
			menu.setParentId(parentId);
			
			Menu m=null;
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			try {
				m = baseService.saveMenu(menu);
			} catch (Exception e) {
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】菜单【"+name+"】失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】菜单【"+name+"】成功！", 0);
			}
			
			String result = formatToJson(m);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}

		//删除菜单
		@RequestMapping("knowledgeEditDataDeleteMent")
		public void toKnowledgeEditDataDeleteMent(HttpServletRequest request,HttpServletResponse response) throws Exception {
			
			int id =Integer.parseInt(request.getParameter("id"));
			Menu menu=publicService.findMenu(id);
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			try {
				baseService.deleteMenu(menu);
			} catch (Exception e) {
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】菜单【"+menu.getName()+"】失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】菜单【"+menu.getName()+"】成功！", 0);
			}
			
			String result = formatToJson(1);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		//修改菜单
		@RequestMapping("knowledgeEditDataUploadMent")
		public void toKnowledgeEditDataUploadMent(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String name =request.getParameter("name");
			String description =request.getParameter("description");
			String url =request.getParameter("url");
			String headline =request.getParameter("headline");
			int sort =Integer.parseInt(request.getParameter("sort"));
			/*int level =Integer.parseInt(request.getParameter("level"))*/;
			int status =Integer.parseInt(request.getParameter("stauts"));
			int parentId =Integer.parseInt(request.getParameter("parentid"));
			int id =Integer.parseInt(request.getParameter("id"));
			Menu menu = publicService.findMenu(id); ;
			menu.setName(name);
			menu.setDescription(description);
			menu.setUrl(url);
			menu.setLevel(1);
			menu.setStatus(status);
			menu.setParentId(parentId);
			menu.setSort(sort);
			menu.setIcon(headline);
			
			Menu m=null;
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			try {
				m = baseService.editMenu(menu);
			} catch (Exception e) {
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】菜单【"+menu.getName()+"】失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
//				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】菜单【"+menu.getName()+"】成功！", 0);
			}
			
			String result = formatToJson(m);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		//修改菜单查询方法
		@RequestMapping("knowledgeEditDataEditMent")
		public void toKnowledgeEditDataEditMent(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			Menu menu=publicService.findMenu(id);
			List<Menu> menuList = publicService.getMenu();
			LinkedHashMap<String, Object> menuMap = new LinkedHashMap<String, Object>();
			for(Menu m:menuList){
				if(menu.getParentId() == 0){
					menuMap.put("parentName", "根目录");
				}else if(menu.getParentId() == m.getId()){
						menuMap.put("parentName", m.getName());
					}
			}
			menuMap.put("level", menu.getLevel());
			menuMap.put("description", menu.getDescription());
			menuMap.put("name", menu.getName());
			menuMap.put("parentId", menu.getParentId());
			menuMap.put("status", menu.getStatus());
			menuMap.put("url", menu.getUrl());
			menuMap.put("sort", menu.getSort());
			menuMap.put("headline", menu.getIcon());
			String result = formatToJson(menuMap);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		
		/**
		 * 按钮
		 */
		
		
		
		
		/**
		 * 权限
		 */
		//角色列表
		@RequestMapping("knowledgeEditDataQueryJurisdiction")
		public void toKnowledgeEditDataQueryJurisdiction(HttpServletRequest request,HttpServletResponse response) throws Exception {
			List<Role> role= publicService.getRole();
			for(int i=0; i<role.size();i++){
				if(role.get(i).getId() == 1){
					role.remove(i);
				}
			}
			String result = formatToJson(role);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		//菜单列表
		@RequestMapping("knowledgeEditDataJurisdictionMenu")
		public void toKnowledgeEditDataJurisdictionMenu(HttpServletRequest request,HttpServletResponse response) throws Exception {
			int id =Integer.parseInt(request.getParameter("id"));
			List<Permission> permission=publicService.getPermission();
			String menuS ="";
			for(Permission p:permission){
				if(p.getRoleId() == id){
					menuS =p.getMenuIds();
				}
			}
			String[] sourceStrArray=null;
			if(!menuS.equals("")){
				sourceStrArray = menuS.split(",");
			}
				List<Menu> menu = publicService.getMenu();
				List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
				LinkedHashMap<String, Object> mp = null;
				for (int i = 0; i < menu.size(); i++) {
					Menu m = menu.get(i);
					mp = new LinkedHashMap<String, Object>();
					mp.put("id", m.getId());
					mp.put("name", m.getName());
					mp.put("pId", m.getParentId());
					if(sourceStrArray!=null){
						for (int j =0;j< sourceStrArray.length; j++){
							if(sourceStrArray[j]!=null && sourceStrArray[j]!=""){
								if(Integer.parseInt(sourceStrArray[j]) == m.getId()){
									mp.put("page", 0);
								}
							}
						}
					}
					r.add(mp);
				}
			String result = formatToJson(r);
			//System.out.println(result);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		// 插入  权限表
		@RequestMapping("menuEditSave")
		@ResponseBody
		public void menuEditSave(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			// System.out.println("dictAddandUpdate");
			String roleid = request.getParameter("roleid");
			String menuids = request.getParameter("menuids");
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			Permission permission = new Permission();
			permission.setRoleId((Integer.parseInt(roleid)));
			permission.setMenuIds(menuids);
			permission.setMenuIds(menuids);
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,roleId",roleid);
			List<Permission> list=publicService.getPermission(queryParams);
			Permission permission2=null;
			for(Permission p :list){
				p.setMenuIds(menuids);
				int logTime=0;
				Role role=publicService.findRole(Integer.parseInt(roleid));
				try {
					baseService.editPermission(p);
				} catch (Exception e) {
//					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】角色【"+role.getName()+"】下菜单失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
//					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】角色【"+role.getName()+"】下菜单成功！", 0);
				}
			
			}
			if(list.size()<1){
				int logTime=0;
				Role role=publicService.findRole(Integer.parseInt(roleid));
				try {
					permission2 =baseService.savePermission(permission);
				} catch (Exception e) {
//					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】角色【"+role.getName()+"】下菜单失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
//					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】角色【"+role.getName()+"】下菜单成功！", 0);
				}
			}
			
			String result = formatToJson(permission2);
			// System.out.println("resultadd    Dict 插入  更新：" + result);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		
		/**
		 * 百度富文本上传
		 * @param request
		 * @param response
		 */
		@RequestMapping("ueditorController")
		public void controller(HttpServletRequest request,HttpServletResponse response) {
	        try {
	        	response.setContentType("application/json");   
	    		request.setCharacterEncoding( "utf-8" );
	    		response.setHeader("Content-Type" , "text/html");
	            String rootPath = request.getSession().getServletContext().getRealPath("/");
	        	String a = request.getRequestURI();
	            String exec = new ActionEnter(request, rootPath).exec();
	            String uploadPath = FILESERVER;
	            JSONObject jb = JSONObject.fromObject(exec);
	    	    Map map = (Map)jb;
	    	    
	    	    Object list = map.get("list");
	    	    Object url = map.get("url");
	    	    if(list!=null){
	    	    	List<Map<String,Object>>ll=(List<Map<String,Object>>)list;
	    	    	for (Map<String, Object> map2 : ll) {
	    	    		map2.put("url",  uploadPath+map2.get("url").toString());
					}
	    	    	
	    	    }
	    	    if(url!=null){
	    	    	map.put("url", uploadPath+map.get("url").toString());
	    	    }
	    	    /*for (Object key : map.keySet()) {
	    	    	String url =uploadPath+map.get(key).toString();
	    	    	if("url".equals(key)){
	    	    		map.put(key, url);
	    	    	}
	    	    }*/
	    	    exec = JSONObject.fromObject(map).toString();
	            PrintWriter writer = response.getWriter();
	            writer.write(exec);
	            writer.flush();
	            writer.close();
	        } catch (Exception e) {
	                e.printStackTrace();
	        }
		}	
}
